﻿using System.Runtime.CompilerServices;

namespace Utils
{
    public class TimedAwaiter : INotifyCompletion
    {
        private WheelTimer _wheelTimer;

        private Action _continuation;

        public bool IsCompleted { get; private set; }

        public TimedAwaiter(WheelTimer wheelTimer)
        {
            _wheelTimer = wheelTimer;
        }

        public void OnCompleted(Action continuation)
        {
            _continuation = continuation;
            if (IsCompleted)
                Interlocked.Exchange(ref _continuation, null)?.Invoke();
        }

        public void Run()
        {
            IsCompleted = true;
            Interlocked.Exchange(ref _continuation, null)?.Invoke();
        }

        public TimedAwaiter GetAwaiter()
        {
            return this;
        }

        public object GetResult()
        {
            return null;
        }

    }
}
